package com.ljd.controller;


import com.ljd.entity.EduChapter;
import com.ljd.entity.vo.ChapterVo;
import com.ljd.service.EduChapterService;
import com.ljd.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.PushbackInputStream;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(description = "章节列表")
//@CrossOrigin
public class EduChapterController {
    @Autowired
    EduChapterService eduChapterService;

    @ApiOperation("查询章节和小结")
    @GetMapping("/getChapterAndVideo/{courseId}")
    public R getChapterAndVideo(@PathVariable String courseId){

        List<ChapterVo> chapterVos = eduChapterService.getChapterAndVideo(courseId);
        if(chapterVos == null){
            return R.error().message("查询失败");
        }
        return R.ok().data("chapterVos",chapterVos);
    }

    @ApiOperation("添加章节")
    @PostMapping("/saveChapter")
    public R saveChapter(@RequestBody EduChapter chapter){
         eduChapterService.saveChapter(chapter);
         return R.ok();
    }

    @ApiOperation("修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter){
        eduChapterService.updateChapter(chapter);
        return R.ok();
    }

    @ApiOperation("查询章节")
    @GetMapping("getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        EduChapter chapter = eduChapterService.getChapter(chapterId);
        return R.ok().data("chapter", chapter);
    }

    @ApiOperation("删除章节")
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        Boolean isSucceed = eduChapterService.deleteChapter(chapterId);
        if(isSucceed){
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }
}

